//
//  KLChannelViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLChannelViewController.h"
#import "KLChannelCityViewController.h"
@interface KLChannelViewController ()
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) NSArray                               *titleArray;
@property(nonatomic, strong) NSMutableArray<NSDictionary*>                     *provinceArray;
@property(nonatomic, strong) NSMutableArray<NSDictionary*>                     *cityArray;
@end

@implementation KLChannelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地址选择";
    [self configSubView];
    [self sendLoadProvinveRequest];
    
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    _titleArray = @[@"省",@"市",@"区域",@"乡"];
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, 44)
                                                         andTitle:_titleArray handle:^(STSegementButton *sender) {
                                                             
                                                             
                                                         }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleColor = KL_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    self.segementButton.userInteractionEnabled = NO;
    //  [self.view addSubview:self.segementButton];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.provinceArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    NSDictionary *dic;
    dic = self.provinceArray[indexPath.row];
    cell.textLabel.text = dic[@"name"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.provinceArray[indexPath.row];
    [self sendCityWithRequestWithDic:dic];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    [self configSubView];
    [self sendLoadProvinveRequest];
}
- (void)sendLoadProvinveRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:@"0" forKey:@"type"];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/user/address"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"resqust"]) {
                                                     self.provinceArray = [NSMutableArray new];
                                                     self.provinceArray = dataDic[@"resqust"];
                                                 }
                                                 
                                                 [self.tableView reloadData];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView reloadData];
                                                 
                                             }];
}
- (void)sendCityWithRequestWithDic:(NSDictionary*)paramDic{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:@"1" forKey:@"type"];
    NSString * code = paramDic[@"code"];
    NSString * name = paramDic[@"name"];
    [dic setObject:code forKey:@"code"];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/user/address"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"resqust"]) {
                                                     self.cityArray = [NSMutableArray new];
                                                     self.cityArray = dataDic[@"resqust"];
                                                 }
                                                 
                                                 if (self.cityArray.count) {
                                                     KLChannelCityViewController * vc = [KLChannelCityViewController new];
                                                     vc.lastPlace = name;
                                                     vc.lastCode = code;
                                                     vc.dataSouce = self.cityArray;
                                                     [self.navigationController pushViewController:vc animated:YES];
                                                 }else{
                                                     //支付
                                                 }
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView reloadData];
                                                 
                                             }];
}


@end

